/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package handler;

import dto.Student;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author NDH
 */
public class MyHandler extends DefaultHandler {

    int att = 0;
    boolean fNew = true;
    boolean fCode = false;
    boolean fName = false;
    boolean fMail = false;
    Student currentS;
    public  ArrayList<Student> listS;
    @Override
    public void startDocument() throws SAXException {
        super.startDocument(); //To change body of generated methods, choose Tools | Templates.
      listS = new ArrayList<Student>();

    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument(); 
          listS.size();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes); //To change body of generated methods, choose Tools | Templates.
        // System.out.println("qName" + qName);

        if (qName.equals("student")) {
            currentS = new Student();
            att = 0;
        } else if (qName.equals("code")) {
            //currentS.setCode(Integer.parseInt(attributes.));
            att = 1;
        } else if (qName.equals("name")) {
            att = 2;
        } else if (qName.equals("email")) {
            att = 3;
        }


    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String s = new String(ch, start, length);
        switch (att) {
//            case 0:
//                currentS = new Student();
//                break;
            case 1:
                currentS.setCode(Integer.parseInt(s));
                att = 9;
                break;
            case 2:
                currentS.setName(s);
                att = 9;
                break;
            case 3:
                currentS.setEmail(s);
                listS.add(currentS);
                att =9 ;
                break;
        }
//        if (fCode) {
//            
//        }
    }

    @Override
    public void skippedEntity(String name) throws SAXException {
        super.skippedEntity(name); //To change body of generated methods, choose Tools | Templates.
    }
}
